﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using Longbow.Caching;

namespace HuaCheng.DataAccess
{
    /// <summary>
    /// 跳转原因的数据库操作
    /// </summary>
    public class JumpReasonRecordControl
    {
        internal const string RetrieveJumpLogDataKey = "JumpReasonRecordControl-RetrieveJumpLog";
        /// <summary>
        /// 工步跳转时将跳转原因插入数据库
        /// </summary>
        /// <param name="jumpReasonRecord"></param>
        public static void InsertNewJumpReason(JumpReasonRecord jumpReasonRecord)
        {
            string sql = "insert into jump_log(test_id,dev_ip,cabinet_id,mid_id,chl_id,step_index,circ,jump_time,jump_reason,jump_result)" +
                " values (@testid,@devip,@gid,@mid,@chlid,@stepindex,@circ,@jumpTime,@jumpReason,@jumpResult)";
            try
            {
                using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
                {
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@testid", jumpReasonRecord.TestId));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@devip", jumpReasonRecord.DevIp));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@gid", jumpReasonRecord.GID));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@mid", jumpReasonRecord.MID));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@chlid", jumpReasonRecord.ChlId));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@stepindex", jumpReasonRecord.StepIndex));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@circ", jumpReasonRecord.Circ));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@jumpTime", jumpReasonRecord.JumpTime));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@jumpReason", jumpReasonRecord.JumpReason));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@jumpResult", jumpReasonRecord.JumpResult));
                    DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
                }
            }
            catch (Exception ex) { Console.WriteLine(ex.Message + ex.StackTrace); }
        }
        public static List<JumpReasonRecord> RetrieveJumpLog(string dev_ip, int gid,int mid, int chl_id)
        {
            string key = RetrieveJumpLogDataKey;
            return CacheManager.GetOrAdd(key, k =>
            {
                List<JumpReasonRecord> ret = new List<JumpReasonRecord>();
                try
                {
                    string sql = "select * from jump_log where dev_ip=@dev_ip and cabinet_id=@gid and mid_id=@mid and chl_id=@chl_id";
                    DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@dev_ip", dev_ip));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@gid", gid));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@mid", mid));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@chl_id", chl_id));
                    using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
                    {
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                JumpReasonRecord jump = new JumpReasonRecord()
                                {
                                    TestId = Convert.ToString(reader["test_id"]),
                                    DevIp = Convert.ToString(reader["dev_ip"]),
                                    GID = Convert.ToInt32(reader["cabinet_id"]),
                                    MID = Convert.ToInt32(reader["mid_id"]),
                                    ChlId = Convert.ToInt32(reader["chl_id"]),
                                    StepIndex = Convert.ToInt32(reader["step_index"]),
                                    Circ = Convert.ToString(reader["circ"]),
                                    JumpTime = Convert.ToString(reader["jump_time"]),
                                    JumpReason = Convert.ToString(reader["jump_reason"]),
                                    JumpResult = Convert.ToString(reader["jump_result"]),
                                };
                                ret.Add(jump);
                            }
                        }
                    }
                }
                catch (Exception ex) { Console.WriteLine(ex.Message + ex.StackTrace); }
                return ret;
            }, RetrieveJumpLogDataKey);
        }
    }
}
